package com.ymxx.jweb.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class HttpUtil {

	private HttpUtil(){}
	/**
	 * 根据远程url计算远程文件的名称
	 * @param path
	 * @return
	 */
	private static String getRemoteFileName(String path){
		String [] str = path.split("/");
		return str[str.length-1] != null?str[str.length-1]:null;
	}
	/**
	 * 下载文件到指定的目录
	 * @param destUrl
	 * @param dirName
	 * @throws Exception
	 */
	public static Long download(String destUrl,String dirName) throws Exception { 
		File dir = new File(dirName);
		if(!dir.exists()){
			dir.mkdir();
		}
		FileOutputStream fos = null;   
		BufferedInputStream bis = null;   
		HttpURLConnection httpUrl = null;	
		URL url = null;   
		byte[] buf = new byte[809600]; 
		int size = 0;
		long totalSize = 0;
		url = new URL(destUrl);
		String fileName = new String(getRemoteFileName(url.getFile()));
		httpUrl = (HttpURLConnection) url.openConnection();
		if(httpUrl == null){
			return -1l;
		}
		httpUrl.connect();
		totalSize = httpUrl.getContentLength();
		if(totalSize <= 0){
			return -1l;
		}
		
		bis = new BufferedInputStream(httpUrl.getInputStream());   
		
		fos = new FileOutputStream(dirName+"/"+fileName);   
	     
	
		while ( (size = bis.read(buf)) != -1){
			fos.write(buf, 0, size);
		}
		fos.close();   
		bis.close();   
		httpUrl.disconnect();
		return totalSize;
	}
	
	/**
	 * @param webpageAddr
	 * @return
	 * @throws Exception
	 */
	public static String getHtmlContent(String webpageAddr)
			throws Exception{
		
		return getHtmlContent(webpageAddr,null,null).get(0);
	}
	/**
	 * 解析远程文件的内容
	 * @param webpageAddr
	 * @param regex
	 * @return
	 * @throws Exception
	 */
	public static List<String> getHtmlContent(String webpageAddr,String regex)
			throws Exception{
		
		return getHtmlContent(webpageAddr,regex,UsefullURLS.URL_ENCODE);
	}
	/**
	 * @param webpageAddr
	 * @param regex
	 * @param encode
	 * @return
	 * @throws Exception
	 */
	/**
	 * @param webpageAddr
	 * @param regex
	 * @param encode
	 * @return
	 * @throws Exception
	 */
	public static List<String> getHtmlContent(String webpageAddr,String regex,String encode)
			throws Exception{
		StringBuffer sb = new StringBuffer(1024*10*8);
		encode = encode == null?UsefullURLS.URL_ENCODE:encode;
		URL url = new URL(webpageAddr);
		url.openConnection().setConnectTimeout(10000);
		InputStream in = url.openStream();
		byte[] buff = new byte [80960];
		while((in.read(buff)) != -1){
			sb.append(new String(buff,encode));
		}
		in.close();
		
		String temp = null;
		List<String> list = new ArrayList<String>();
		if(regex != null){
			Pattern pattern = Pattern.compile(regex);//
			Matcher matcher = pattern.matcher(sb.toString());//
			while(matcher.find()){
				temp = matcher.group();
				list.add(temp); 
			}
		}else{
			list.add(sb.toString());
		}
		
		return list;
	}
}
